HAL GPIO initialization and configuration functions migration ¶
The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:
Activate a pin from pinout panel.
Open STM32CubeMX2 GPIO configuration panel.
Configure the SW layer HAL.
Configure the GPIO main peripheral configuration.
Generate the code.
Note
The configuration below might varies from a series to another.
HAL GPIO configuration panel ¶
Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.
HAL GPIO main features configuration ¶
Configure the GPIO main features.
Generated GPIO initialization sequence ¶
The GPIO initialisation sequence is generated in the
mx_gpio_default_init
function in
mx_gpio_default.c
file
under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
Related concepts:
|
Topic |
HAL1 Code Snippet |
HAL2 Code Snippet |
|---|---|---|
|
Initialization and configuration sequence |
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
|
system_status_t mx_gpio_default_init(void)
{
hal_gpio_config_t gpio_config;
HAL_RCC_GPIOA_EnableClock();
/* Configure PA0 GPIO pin in input mode */
gpio_config.mode = HAL_GPIO_MODE_INPUT;
gpio_config.pull = HAL_GPIO_PULL_NO;
if (HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_0, &gpio_config) != HAL_OK)
{
return SYSTEM_PERIPHERAL_ERROR;
}
return SYSTEM_OK;
}
|
|
Deinitialization sequence |
void MX_GPIO_DeInit(void)
{
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
}
|
system_status_t mx_gpio_default_deinit(void)
{
HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_0);
return SYSTEM_OK;
}
|
GPIO pin configuration as alternate ¶
Create a new project with the STM32CubeMX2, then start the initialization and configuration.
GPIO pin selection ¶
Select PA0 as reserved pin.
Select PA0 pin as TIM2_CH1 alternate.
Select CC1 GPIO pin, in the TIM2 configuration.
Generated GPIO initialization sequence ¶
The GPIO initialisation sequence is generated in the
mx_tim2_init
function in
mx_tim2.c
file under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
|
Topic |
HAL1 Code Snippet |
HAL2 Code Snippet |
|---|---|---|
|
Initialization and configuration sequence |
void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM2)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM2 GPIO Configuration
PA0 ------> TIM2_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
|
hal_tim_handle_t *mx_tim2_init(void)
{
hal_tim_config_t config;
if (HAL_TIM_Init(&hTIM2, HAL_TIM2) != HAL_OK)
{
return NULL;
}
HAL_RCC_TIM2_EnableClock();
/* Timer configuration to reach the output frequency at 2441 Hz */
config.prescaler = 0;
config.counter_mode = HAL_TIM_COUNTER_UP;
config.period = 0xFFFF;
config.repetition_counter = 0;
config.clock_sel.clock_source = HAL_TIM_CLK_INTERNAL;
if (HAL_TIM_SetConfig(&hTIM2, &config) != HAL_OK)
{
return NULL;
}
/* Configure Channels */
hal_tim_oc_channel_config_t oc_config;
oc_config.polarity = HAL_TIM_OC_HIGH;
oc_config.idle_state = HAL_TIM_OC_IDLE_STATE_RESET;
if (HAL_TIM_OC_SetConfigChannel(&hTIM2,
HAL_TIM_CHANNEL_1, &oc_config) != HAL_OK)
{
return NULL;
}
hal_tim_oc_compare_unit_config_t oc_compare_unit_config;
oc_compare_unit_config.mode = HAL_TIM_OC_FROZEN;
oc_compare_unit_config.pulse = 0x00000000;
if (HAL_TIM_OC_SetConfigCompareUnit(&hTIM2,
hal_tim_oc_channel_to_compare_unit(HAL_TIM_CHANNEL_1),
&oc_compare_unit_config) != HAL_OK)
{
return NULL;
}
/* Any events generate an update interrupt or DMA request if enabled (default) */
/* HAL_TIM_SetUpdateSource(&hTIM2, HAL_TIM_UPDATE_REGULAR); */
/* UEV event generation is enabled by default */
/* HAL_TIM_EnableUpdateGeneration(&hTIM2); */
/* Configure Master mode */
/* HAL_TIM_SetTriggerOutput(&hTIM2, HAL_TIM_TRGO_RESET); */
/* Configure Encoder interface */
/* Configure the Pulse generator */
hal_tim_pulse_generator_config_t pulse_generator;
pulse_generator.pulse_width = 0;
pulse_generator.prescaler = HAL_TIM_PULSE_DIV1;
HAL_TIM_OC_SetPulseGenerator(&hTIM2, &pulse_generator);
/* HAL_TIM_SetCaptureCompareDMAReqSource(&hTIM2, HAL_TIM_CC_DMAREQ_CC); */
HAL_TIM_SetLockLevel(&hTIM2, HAL_TIM_LOCK_);
/* GPIO configuration for channel1 */
HAL_RCC_GPIOA_EnableClock();
hal_gpio_config_t gpio_config;
/**
TIM2 GPIO Configuration
HW labels: PA0 GPIO Configuration
PA0
---------> TIM2_CH1
**/
gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;
gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
gpio_config.pull = HAL_GPIO_PULL_NO;
gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;
gpio_config.alternate = HAL_GPIO_AF_1;
HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_0, &gpio_config);
return &hTIM2;
}
|
|
Deinitialization sequence |
void MX_GPIO_DeInit(void)
{
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
}
|
void mx_tim2_deinit(void)
{
(void)HAL_TIM_DeInit(&hTIM2);
HAL_RCC_TIM2_Reset();
HAL_RCC_TIM2_DisableClock();
/* De-initialize all GPIO pins associated with TIM2 */
/* Deinitialization of TIM2: TIM2_CH1 */
HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_0);
}
|
EXTI line configuration to specific GPIO pin ¶
Create a new project with the STM32CubeMX2, then start the initialization and configuration.
GPIO pin selection ¶
Select PA0 as configured pin.
GPIO mode, pull and EXTI line configuration ¶
Select GPIO pin mode as Input.
Select GPIO pin pull as No pull-upand no pull-down.
Enable EXTI line.
Configure EXTI trigger.
Generated GPIO initialization sequence ¶
The GPIO initialization sequence is generated in the
mx_gpio_default_init
and
mx_gpio_default_deinit
functions in the
mx_gpio_default.c
file under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
|
Topic |
HAL1 Code Snippet |
HAL2 Code Snippet |
|---|---|---|
|
Initialization and configuration sequence |
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
}
|
system_status_t mx_gpio_default_init(void)
{
hal_gpio_config_t gpio_config;
HAL_RCC_GPIOA_EnableClock();
gpio_config.mode = HAL_GPIO_MODE_INPUT;
gpio_config.pull = HAL_GPIO_PULL_NO;
if (HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_0, &gpio_config) != HAL_OK)
{
return SYSTEM_PERIPHERAL_ERROR;
}
hal_exti_config_t exti_config;
/* Initialize the EXTI for line 0 */
HAL_EXTI_Init(&hEXTI0, HAL_EXTI_LINE_0);
/* Set the trigger as RISING for the GPIOA */
exti_config.trigger = HAL_EXTI_TRIGGER_RISING;
exti_config.gpio_port = HAL_EXTI_GPIOA;
HAL_EXTI_SetConfig(&hEXTI0, &exti_config);
/* Set line 0 Interrupt priority */
HAL_CORTEX_NVIC_SetPriority(EXTI0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0,
HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(EXTI0_IRQn);
return SYSTEM_OK;
}
|
|
Deinitialization sequence |
void MX_GPIO_DeInit(void)
{
/* set line 0 Interrupt priority */
HAL_NVIC_DisableIRQ(EXTI0_IRQn);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
}
|
system_status_t mx_gpio_default_deinit(void)
{
/* De-initialize the EXTI for GPIOA line0 */
HAL_EXTI_DeInit(&hEXTI0);
/* set line 0 Interrupt priority */
HAL_CORTEX_NVIC_DisableIRQ(EXTI0_IRQn);
HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_0);
return SYSTEM_OK;
}
|